package com.lp.mgt.services.impls;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lp.mgt.entities.Department;
import com.lp.mgt.entities.Menu;
import com.lp.mgt.mappers.DepartmentMapper;
import com.lp.mgt.services.DepartmentService;
import com.lp.mgt.services.PostService;
import com.lp.mgt.services.UserService;
import com.lp.mgt.utils.TreeUtils;
import com.lp.mgt.utils.UUIDUtil;
@Service
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired
	private DepartmentMapper deptMapper;
	@Autowired
	private UserService userService;
	@Autowired
	private PostService postService;
	
	@Override
	public List<Department> getDepartmentsToLoadTree(Department dept) {
		List<Department> list= deptMapper.getDepartmentsToLoadTree(dept);
		if(list.size()<1) {
			return null;
		}
		//创建根节点
		Department node=new Department();
		node.setDeptId("0");
		node.setDeptName("MGT部门管理树");
		node.setOrderNum("0");

		for(Department nd:list) {
			if(nd.getParentId()==null ||"".equals(nd.getParentId())) {
				nd.setParentId(node.getDeptId());
			}
		}
		list.add(node);
		return list;
	}

	@Override
	public String insertNewDepartment(Department dept) {
		String id=null;
		if(dept!=null) {
			id=UUIDUtil.getUUID();
			dept.setDeptId(id);
		}
		Integer num=deptMapper.insertNewDepartment(dept);
		return num==1?id:null;
	}

	@Override
	public Boolean updateDepartmentWhenEdit(Department dept) {
		Integer num=deptMapper.updateDepartmentWhenEdit(dept);
		return num==1?true:false;
	}

	@Override
	public Boolean deleteDepartmentById(Department dept) {
		//删除下面的用户和对应职位
		userService.removeUserByDeptId(dept.getDeptId());//删除部门下的用户
		//删除部门关联的岗位
		postService.removePostByDeptId(dept.getDeptId());
		Integer num=deptMapper.deleteDepartmentById(dept);
		return num==1?true:false;
	}

	@Override
	public String[] getChildsIdsWithParentId(String deptId) {
		String ids=getChildDeptIds(deptId,"");
		return ids.split(",");
	}

	//递归获取树当前节点及其子节点的主键
	private String getChildDeptIds(String id,String ids){
		if("".equals(ids)){
			ids=id;
		}
		List<String> childLists=this.getChildDeptByParentID(id);		
		if(childLists.size()>0){
			for(int i=0;i<childLists.size();i++){
				ids+=","+childLists.get(i);//String.join(",",childLists);
				ids=getChildDeptIds(childLists.get(i), ids);
			}
		}
		return ids;
	}
		//根据父级id获取子节点
		private List<String> getChildDeptByParentID(String fjid){
			return deptMapper.getChildDeptByParentID(fjid);
		}

		@Override
		public List<Department> getDept2Tree(Department dept) {
			List<Department> list= deptMapper.getDepartmentsToLoadTree(dept);
			if(list.size()<1) {
				return null;
			}
			for(Department nd:list) {
				nd.setId(nd.getDeptId());
				nd.setText(nd.getDeptName());
			}
			return TreeUtils.getChildPerms(list,"","parentId","id","setNodes");
		}
		
@Override
public Boolean departmentDragAndDrop(Department dept) {
	if(dept!=null) {
		//先查出所有兄弟节点但不包含自己
		List<Department> bros=deptMapper.getBrothersByParentId(dept);
		bros.add(dept);//加入拖拽的节点
		//排序
		Collections.sort(bros, new Comparator<Department>() {
			@Override
			public int compare(Department d1, Department d2) {
				if(Double.valueOf(d1.getOrderNum())-Double.valueOf(d2.getOrderNum())>0) {
					return 1;
				}
				return -1;
			}
			
		});
		//排序号重新赋值因为前段给的0.5
		for (int i=1;i<=bros.size();i++) {
			bros.get(i-1).setOrderNum(String.valueOf(i));
		}
		//批量更新同级节点
		deptMapper.batchUpdateDepartment(bros);
		return true;
	}
	return false;
}


}